﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using KLS.PPDS.Core.Entity;
using System.Xml.Linq;

namespace KLS.PPDS.ApplicationService.Manager
{
    /// <summary>
    /// UploadFileTypeManager
    /// </summary>
    public class UploadFileTypeManager
    {
        /// <summary>
        /// GetAllUploadFileType
        /// </summary>
        /// <returns></returns>
        public static List<UploadFileType> GetAllUploadFileType()
        {
            var xmlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"FileTypeConfig.xml");
            var uploadFileTypelist = new List<UploadFileType> ();
            if (File.Exists(xmlFile))
            {
                var loaded = XDocument.Load(xmlFile);
                var q = from c in loaded.Descendants("file")
                        select new
                        {
                            ext = (string)c.Attribute("ext").Value,
                            type = (string)c.Attribute("type").Value,
                            mime = (string)c.Attribute("mime").Value
                        }
                        ;

                var uploadFileType = new UploadFileType();
                 
                foreach (var obj in q) 
                {
                    uploadFileType = new UploadFileType();
                    uploadFileType.Extension = obj.ext;
                    uploadFileType.Type = obj.type;
                    uploadFileType.Mime = obj.mime;

                    uploadFileTypelist.Add(uploadFileType);
                }
            }
            return uploadFileTypelist;
        }

        /// <summary>
        /// IsValidFile
        /// </summary>
        /// <param name="file"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsValidFile(string file, string type) 
        {
            var result = false;
            var ext = Path.GetExtension(file).ToLower();

            var x = GetAllUploadFileType().Where(c => c.Type.Equals(type) && c.Extension.Equals(ext)).FirstOrDefault();

            if (x != null && String.IsNullOrEmpty(x.Mime).Equals(false)) 
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// IsValidTextFile
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool IsValidTextFile(string file)
        {
            return IsValidFile(file,"txt");
        }

        /// <summary>
        /// IsValidImageFile
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool IsValidImageFile(string file)
        {
            return IsValidFile(file, "img");
        }

        /// <summary>
        /// GetMimeTypeFromExtension
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetMimeTypeFromExtension(string fileName) 
        {
            string ext = Path.GetExtension(fileName);

            var result = "application/octet-stream";
            var x = GetAllUploadFileType().Where(c => c.Extension.ToLower().Equals(ext.ToLower())).FirstOrDefault();

            if (x != null && String.IsNullOrEmpty(x.Mime).Equals(false)) 
            {
                return x.Mime;
            }

            return result;
        }

        /// <summary>
        /// GetSupportedTextFileExtForUploadify
        /// </summary>
        /// <returns></returns>
        public static string GetSupportedTextFileExtForUploadify() 
        {
            var list = GetAllUploadFileType().Where(c => c.Type.Equals("txt")).ToList();

            var rslt = string.Empty;
            foreach (var item in list) 
            {
                rslt += "*"+ item.Extension +";" ;
            }

            return  rslt.Remove(rslt.Length - 1, 1);

            //return rslt;
        }

        /// <summary>
        /// GetSupportedImageFileExtForUploadify
        /// </summary>
        /// <returns></returns>
        public static string GetSupportedImageFileExtForUploadify()
        {
            var list = GetAllUploadFileType().Where(c => c.Type.Equals("img")).ToList();

            var rslt = string.Empty;
            foreach (var item in list)
            {
                rslt += "*" + item.Extension + ";";
            }

            return rslt.Remove(rslt.Length - 1, 1);

            //return rslt;
        }

    }
}
